package com.youlin.controller;

import com.youlin.dto.*;
import com.youlin.dto.req.AddAMReqDto;
import com.youlin.dto.req.FindAllAmReqDto;
import com.youlin.dto.req.FindAllRelationReqDto;
import com.youlin.dto.req.UpdAMStatusReqDto;
import com.youlin.dto.res.AppointmentResDto;
import com.youlin.dto.res.RelationResDto;
import com.youlin.entities.BygAdmin;
import com.youlin.entities.BygFarewellHall;
import com.youlin.service.BygAppointmentService;
import com.youlin.service.BygFarewellHallService;
import com.youlin.service.BygRelationService;
import com.youlin.untils.AdminUtil;
import com.youlin.untils.MyBeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 告别会管理
 * @author ${author}
 * @since 2020-05-28
 */
@ApiSort(6)
@Api(tags = {"6 - 告别会管理"})
@RestController
@RequestMapping("/bygAM")
public class BygAppointmentController {

    @Autowired
    private BygAppointmentService bygAppointmentService;
    @Autowired
    private BygRelationService bygRelationService;
    @Autowired
    private BygFarewellHallService bygFarewellHallService;

    /**
     * 获取当前账号权限下的全部预约信息列表
     * @param baseReqDto
     * @return
     */
    @PostMapping("/common/findAllAppointments")
    @ApiOperationSort(1)
    @ApiOperation(value = "1 - 获取当前账号权限下的全部预约信息列表")
    public PageDataResDto<List<AppointmentResDto>> findAllAppointments (@RequestBody PageDataReqDto<FindAllAmReqDto> baseReqDto) {
        FindAllAmReqDto data = baseReqDto.getData();
        PageInfo pageInfo = baseReqDto.getPageInfo();
        BygAdmin currentAdmin = AdminUtil.getCurrentAdmin();
        return bygAppointmentService.findAllAppointments (currentAdmin, data, pageInfo);
    }

    /**
     * 获取全部告别厅列表
     * @return
     */
    @PostMapping("/common/findAllFHs")
    @ApiOperationSort(2)
    @ApiOperation(value = "2 - 获取全部告别厅列表")
    public BaseDataResDto<List<BygFarewellHall>> findAllFHs () {
        BygAdmin currentAdmin = AdminUtil.getCurrentAdmin();
        return bygFarewellHallService.findAllFHs (currentAdmin.getUnitId());
    }

    /**
     * 逻辑删除预约信息
     * @param baseReqDto
     * @return
     */
    @PostMapping("/operate/delAppointment")
    @ApiOperationSort(3)
    @ApiOperation(value = "3 - 逻辑删除预约信息", notes = "请求参数：data = 预约ID\n" +
            "响应参数：status-message = {1022-只能删除新建状态的告别会}")
    public BaseResDto delAppointment (@RequestBody BaseReqDto<Integer> baseReqDto) {
        Integer amId = baseReqDto.getData();
        if (amId == null) return new BaseResDto(Status.PARAMETERERROR);
        return bygAppointmentService.delAppointment (amId);
    }

    /**
     * 新建或修改预约
     * @param baseReqDto
     * @return
     */
    @PostMapping("/operate/editAppointment")
    @ApiOperationSort(4)
    @ApiOperation(value = "4 - 新建或修改预约", notes = "响应参数：status-message = {1014-告别厅使用时间冲突, " +
            "1015-逝者生日或忌日日期选择错误, 1016-告别厅使用时间选择错误，" +
            "1025-逝者姓名+承办人姓名+承办人手机号完全重复, 1028-逝者与承办人关系选择错误}")
    public BaseResDto editAppointment (@RequestBody BaseReqDto<AddAMReqDto> baseReqDto) {
        AddAMReqDto data = baseReqDto.getData();
        if (MyBeanUtil.isParamError(data)) return new BaseResDto(Status.PARAMETERERROR);
        BygAdmin currentAdmin = AdminUtil.getCurrentAdmin();
        return bygAppointmentService.editAppointment (currentAdmin, data);
    }

    /**
     * 条件获取全部关系信息列表
     * @param baseReqDto
     * @return
     */
    @PostMapping("/operate/findAllRelations")
    @ApiOperationSort(5)
    @ApiOperation(value = "5 - 条件获取全部关系信息列表")
    public BaseDataResDto<List<RelationResDto>> findAllRelations (
            @RequestBody BaseReqDto<FindAllRelationReqDto> baseReqDto) {
        FindAllRelationReqDto data = baseReqDto.getData();
        return bygRelationService.findAllRelations (data);
    }

    /**
     * 修改预约信息状态：1-确定预约、2-仪式完成、3-审核管理
     * @param baseReqDto
     * @return
     */
    @PostMapping("/common/updAMStatus")
    @ApiOperationSort(6)
    @ApiOperation(value = "6 - 修改预约信息状态：1-确定预约、2-仪式完成、3-审核管理、4-审核通过 \n",
            notes = "响应参数：status-message = {1030-修改告别会状态只能逐级递增，" +
                    "1031-告别会预约信息未完善不能确定预约，1032-告别会未结束不能仪式完成}")
    public BaseResDto updAMStatus (@RequestBody BaseReqDto<UpdAMStatusReqDto> baseReqDto) {
        UpdAMStatusReqDto data = baseReqDto.getData();
        if (MyBeanUtil.isParamError(data)) return new BaseResDto(Status.PARAMETERERROR);
        return bygAppointmentService.updAMStatus (data);
    }

    /**
     * 扫描二维码判断是否需要承办人登录
     * @param baseReqDto
     * @return
     */
    @PostMapping("/guest/isNeedLogin")
    @ApiOperationSort(7)
    @ApiOperation(value = "7 - 扫描二维码判断是否需要承办人登录",
            notes = "请求参数：data = 预约UUID \n 响应参数：data=是否需要承办人登录")
    public BaseDataResDto<Boolean> isNeedLogin (@RequestBody BaseReqDto<String> baseReqDto) {
        String amUuId = baseReqDto.getData();
        if (StringUtils.isBlank(amUuId)) return new BaseDataResDto(Status.PARAMETERERROR);
        return bygAppointmentService.isNeedLogin (amUuId);
    }

}

